package ch.sv7.tool.dbmanager.db.dialect.sybase;

/**
 * TODO: Document me!
 * 
 * @author svt
 * 
 */
public class SybaseDataTypeConversion {

	private static final String[] conversionMatrix = new String[] {
		"-IIIIUUIIIIIIIIIIIIIUIIIIII",
		"I-IIIUUIIIIIIIIIIIIIUIIIIII", 
		"II-IIUUIIIIIIIIIIIIIUUUEEUU", 
		"IIE-IIIEEEEEEEEEEEEEIIIIIII",
		"IIEI-IIEEEEEEEEEEEEEIIIIIII", 
		"IIUII-IUUUUUUUUUUUUUUUUIIII", 
		"IIUIII-UUUUUUUUUUUUUUUUIIII",
		"IIIEEUU-IIIIIIIIIIIIUUUEEUU", 
		"IIIEEUUI-IIIIIIIIIIIUUUUEUU", 
		"IIIEEUUII-IIIIIIIIIIUUUEEUU",
		"IIIEEUUIII-IIIIIIIIIUUUEEUU", 
		"IIIEEUUIIII-IIIIIIIIUUUEEUU", 
		"IIIEEUUIIIII-IIIIIIIUUUEEUU",
		"IIIEEUUIIIIII-IIIIIIUUUEEUU", 
		"IIIEEUUIIIIIII-IIIIIUUUEEUU", 
		"IIIEEUUIIIIIIII-IIIIUUUEEUU",
		"IIIEEUUIIIIIIIII-IIIUUUEEUU", 
		"IIIEEUUIIIIIIIIII-IIUUUEEUU", 
		"IIIIIUUIIIIIIIIIII-IUUUEEUU",
		"IIIIIUUIIIIIIIIIIII-UUUEEUU", 
		"UUUEEUUUUUUUUUUUUUUU-IUEEUU", 
		"EEEEEUUUUUUUUUUUUUUUI-IUUUU",
		"EEUUUUUUUUUUUUUUUUUUUI-EEUU", 
		"IIEIIIIEEEEEEEEEEEEEIII-III", 
		"IIEIIIIEEEEEEEEEEEEEIIII-II", 
		"IIUIIIUUUUUUUUUUUUUUUUUII-I",
		"IIUIIIUUUUUUUUUUUUUUUUUIII-"};

	private static final String[] types = new String[] { 
		"binary",
		"varbinary",
		"bit",
		"char", 
		"varchar", 
		"datetime", 
		"smalldatetime", 
		"tinyint",
		"smallint", 
		"unsigned smallint", 
		"int", 
		"unsigned int", 
		"bigint", 
		"unsigned bigint", 
		"decimal", 
		"numeric",
		"float", 
		"real", 
		"money", 
		"smallmoney", 
		"text", 
		"unitext", 
		"image", 
		"unichar", 
		"univarchar",
		"date", 
		"time" };

	
	static final char IMPLICIT = 'I';
	static final char EXPLICIT = 'E';
	static final char UNSUPPORTED = 'U';
	static final char SAME = 'S';
	static final char IRRELEVANT = '-';
	
	private static int getTypeIndex(String typeName) {
		for (int i=0; i<types.length; i++) {
			if (types[i].equalsIgnoreCase(typeName)){
				return i;
			}
		}
		return -1;
	}
	
	public static char getConversionType(String fromType, String toType){
		if ("text".equalsIgnoreCase(fromType) || "text".equalsIgnoreCase(toType)
				|| "image".equalsIgnoreCase(fromType) || "image".equalsIgnoreCase(toType) 
				|| "unitext".equalsIgnoreCase(fromType) || "unitext".equalsIgnoreCase(toType)
				){
			return UNSUPPORTED;
		}
				
		if (fromType.equals(toType)){
			return SAME;
		}
		
		int i1 = getTypeIndex(fromType);
		int i2 = getTypeIndex(toType);
		if (i1 < 0 || i2 < 0 ){
			return IRRELEVANT;
		} else {
			
			return conversionMatrix[i1].charAt(i2);
		}
		
		
	}
	
	public static boolean isImplicitConversion(String fromType, String toType){
		char type = getConversionType(fromType, toType);
		return type == IMPLICIT;
	}
	
	public static boolean isUnsupportedConversion(String fromType, String toType){
		char type = getConversionType(fromType, toType);
		return type == UNSUPPORTED;
	}
	
	public static boolean isExplicitConversion(String fromType, String toType){
		char type = getConversionType(fromType, toType);
		return type == EXPLICIT;
	}
	
}
